// pages/detail/detail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    earnMoney:'2.00',
    orderTime:'',
    createTime:'',
    payTime:'',
    receiptTime:"",
    orderNum:'',
    orderStatus:'',
    getAddress: '',
    finalAddress: '',
    orderDetailResultList:{},
    peisongIng:'',
    waitRenling:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var orderId = app.globalData.orderId
    var token = app.globalData.token
    var orderDetailResultList = new Array()
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/orders/' + orderId ,
      method: 'GET',
      header:{
        'content-type': 'application/x-www-form-urlencoded',
        'X-SDX-Token': token
      },
      success:function(res){
        if(res.statusCode ==200){
          console.log("获取到的为：" + res.data.data.apartmentName)
          that.setData({
            finalAddress: res.data.data.apartmentName + res.data.data.address,
            getAddress: res.data.data.apartmentName,
            orderTime: res.data.data.createTime.substring(11, 16),
            createTime: res.data.data.createTime,
            receiptTime: res.data.data.receiptTime,
            payTime: res.data.data.payTime,
            orderNum: res.data.data.code,
            orderStatus: res.data.data.status,
          })
        }
      }
    })
    
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/orders/' + orderId + '/orderdetails',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'X-SDX-Token': token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          console.log("获取到的为：" + res.data.data[0].goodsSsName)
          for(var i=0;i<res.data.data.length;i++){
            orderDetailResultList[i]= res.data.data[i]
          }
          that.setData({
            orderDetailResultList: orderDetailResultList,
          })
        }
      }
    })
    this.setData({
      peisongIng : app.globalData.peisongIng,
      waitRenling: app.globalData.waitRenling
    })
  },
  testSubmit: function (e) {
    let formId = e.detail.formId;
    this.dealFormIds(formId); //处理保存推送码
    console.log(formId)
    var token = app.globalData.token
    let type = e.detail.target.dataset.type;
    console.log('our formIds :' + app.globalData.formIds)
    //根据type的值来执行相应的点击事件
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/formids/',
      data: {
        formId: formId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'X-SDX-Token': token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          console.log('success')
        }
      }
    })
  },
  dealFormIds: function (formId) {
    let formIds = app.globalData.gloabalFomIds;//获取全局数据中的推送码gloabalFomIds数组
    if (!formIds) formIds = [];
    let data = {
      formId: formId,
      expire: parseInt(new Date().getTime() / 1000) + 604800 //计算7天后的过期时间时间戳
    }
    formIds.push(data);//将data添加到数组的末尾
    app.globalData.gloabalFomIds = formIds; //保存推送码并赋值给全局变量
  },
  peisong: function (e) {
    var orderId = app.globalData.orderId
    var token = app.globalData.token
    console.log("our token is:" + token)
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/orders/' + orderId + '/',
      data: {
        status: app.globalData.peisongCompleted
      },
      method: 'PUT',
      header: {
        'content-type': 'application/json',
        'X-SDX-Token': token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          wx.showToast({
            title: '配送完成',
            icon: 'success',
            duration: 1000
          })
          wx.switchTab({
            url: '../index/index',
            success(res) {
              let page = getCurrentPages().pop();
              if (page == undefined || page == null) {
                return
              }
              page.onLoad();
            }
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '配送完成失败',
          content: '请重新操作',
        })
      }
    })
  },
  renling: function (e) {
    var orderId = app.globalData.orderId
    var token = app.globalData.token
    console.log("our token is:" + token)
    wx.request({
      url: 'https://www.cycs023.cn/f50m-web/api/orders/' + orderId + '/',
      data: {
        status: '16'
      },
      method: 'PUT',
      header: {
        'content-type': 'application/json',
        'X-SDX-Token': token
      },
      success: function (res) {
        if (res.statusCode == 200) {
          wx.showToast({
            title: '认领成功',
            icon: 'success',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '认领失败',
          content: '请重新操作',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})